package com.example.leetcode.day;

import com.example.leetcode.ListNode;


/**
 * 给你单链表的头节点 head ，请你反转链表，并返回反转后的链表。
 */
public class Day206 {

    /**
     * 1 2 3
     * r = 1 ,next = 2 ,next1 = 3
     * 2 -> 1
     * @param head
     * @return
     */
    public ListNode reverseList(ListNode head) {
        if(head == null) {
            return head;
        }
        ListNode r = head.next;
        ListNode pre= head;
        if (r == null) {
            return pre;
        }
        pre.next = null;
        while (r != null) {
            ListNode next = r.next;
            r.next = pre;
            pre = r;
            r= next;
        }
        return pre;
    }

    public static void main(String[] args) {
        ListNode node = new ListNode(5);
        ListNode node1 = new ListNode(4, node);
        ListNode node2 = new ListNode(3,node1);
        Day206 d = new Day206();
        d.reverseList(node2);
    }

}
